package com.example.fp.wrt.controller;


import com.example.fp.cgc.code.logAnno.LogAnno;
import com.example.fp.wrt.mapper.LivenessMapper;
import com.example.fp.wrt.entity.Liveness;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;
import org.springframework.web.servlet.ModelAndView;

import java.io.Serializable;
import java.util.List;
/**
 * 编写人：魏仁涛
 * 编写时间：1-4
 * 修改时间：
 */
@Controller
@RequestMapping
public class LivenessWebController {
    @Autowired
    private LivenessMapper livenessMapper;


    @LogAnno(operateType = "查询当前应用排行列表")
    @RequestMapping(value ="/livenesslist")
    public ModelAndView getallLiveness(Model model){
        List<Liveness> livenessList=livenessMapper.selectList(null);
        model.addAttribute("livenessList", livenessList);
        return new ModelAndView("livenessweb/livenesslist", "livenessModel", model);
    }

    @RequestMapping(value ="/livenesssuccess")
    public ModelAndView successLiveness(Model model){
        return new ModelAndView("livenessweb/success", "livenessModel", model);
    }

    @GetMapping(value ="/livenessForm")
    public ModelAndView createLivenessForm(Model model){
        model.addAttribute("liveness", new Liveness());
        model.addAttribute("title", "创建一个新应用");
        return new ModelAndView("livenessweb/livenessadd", "livenessModel", model);
    }



    @PostMapping(value ="/addOrModityLiveness")
    public ModelAndView saveOrUpdateGame(Liveness liveness){
        Liveness liveness1= livenessMapper.selectById(liveness.getId());

        if (liveness1 == null) {
            livenessMapper.insert(liveness);
            return new ModelAndView("redirect:/success");//重定向到list页面
        } else {
            livenessMapper.updateById(liveness);
            return new ModelAndView("redirect:/livenesslist"); //重定向到list页面
        }
    }


    @GetMapping("/deleteLiveness/{id}")
    public ModelAndView deleteLiveness(@PathVariable("id") int id){
        livenessMapper.deleteById(id);
        return new  ModelAndView("redirect:/livenesslist");//重定向到list页面
    }

    @GetMapping("/modifyLiveness/{id}")
    public ModelAndView modifyLiveness(@PathVariable("id") int id,Model model){
        model.addAttribute("liveness", livenessMapper.selectById(id));
        model.addAttribute("title", "修改xx应用");
        return new ModelAndView("livenessweb/modifyLiveness", "livenessModel", model);
    }

}
